package com.mclee.util;

import java.util.Calendar;
import java.text.SimpleDateFormat;
/**
 * - 현재일시
 * - 날짜가감
 * - 날짜차이
 * - 요일정보
 * 
 * @author mclee
 *
 */
public class DateUtil {

	public String getCurrentDateTime(String pForamt) {
		
		Calendar 			cal = Calendar.getInstance();
		SimpleDateFormat	sdf = new SimpleDateFormat(pForamt);
		
		return sdf.format(cal.getTime());
	}
	
	
	
	public String addDate(String pDate, String pAddType, int pAddValue) {
		
		Calendar			cal 	= Calendar.getInstance();
		SimpleDateFormat	sdf 	= new SimpleDateFormat("yyyyMMdd");
		int					year 	= Integer.parseInt(pDate.substring(0, 4));
		int					month	= Integer.parseInt(pDate.substring(4, 6)) - 1;		// Calendar.set 에 설정하는 month 값은 0 베이스이므로 -1을 함.
		int					day		= Integer.parseInt(pDate.substring(6,8));
		
		cal.set(year, month, day);

		if ("y".equals(pAddType)) {
			cal.add(Calendar.YEAR, pAddValue);
		} else if ("m".equals(pAddType)) {
			cal.add(Calendar.MONTH, pAddValue);
		} else if ("d".equals(pAddType)) {
			cal.add(Calendar.DATE, pAddValue);
		} else {
			throw new IllegalArgumentException("The pAddType " + pAddType + " is not valid.");
		}
		
		return sdf.format(cal.getTime());
	}
	
	
	
	public int getDiffDays(String pDate1, String pDate2) {
		
		Calendar 	cal1 = Calendar.getInstance();
		Calendar 	cal2 = Calendar.getInstance();
		int			year;
		int			month;
		int			day;
		long		ms1;
		long		ms2;
		long		diff;
		
		year 	= Integer.parseInt(pDate1.substring(0, 4));
		month	= Integer.parseInt(pDate1.substring(4, 6)) - 1;
		day		= Integer.parseInt(pDate1.substring(6,8));
		cal1.set(year, month, day);
		
		year 	= Integer.parseInt(pDate2.substring(0, 4));
		month	= Integer.parseInt(pDate2.substring(4, 6)) - 1;
		day		= Integer.parseInt(pDate2.substring(6,8));
		cal2.set(year, month, day);
		
		ms1		= cal1.getTimeInMillis();
		ms2		= cal2.getTimeInMillis();
		diff	= ms2 - ms1;
		
		return (int)(diff / (24 * 60 * 60 * 1000));
	}
	
	
	/**
	 * 입력한 날짜의 요일명을 반환한다.
	 * @param pDate - yyyyMMdd 포맷의 날짜값
	 * @return "월", "화", "수", "목", "금", "토", "일" 중의 하나
	 */
	public String getDay(String pDate) {
		
		Calendar	cal 	= Calendar.getInstance();
		int			year 	= Integer.parseInt(pDate.substring(0, 4));
		int			month	= Integer.parseInt(pDate.substring(4, 6)) - 1;		// Calendar.set 에 설정하는 month 값은 0 베이스이므로 -1을 함.
		int			day		= Integer.parseInt(pDate.substring(6,8));
		String		rtn		= "";
		
		cal.set(year, month, day);
		
		switch (cal.get(Calendar.DAY_OF_WEEK)) {	// switch (integer) ... case integer   and break
			case  Calendar.MONDAY:
				rtn = "월";
				break;
			case  Calendar.TUESDAY:
				rtn = "화";
				break;
			case  Calendar.WEDNESDAY:
				rtn = "수";
				break;
			case  Calendar.THURSDAY:
				rtn = "목";
				break;
			case  Calendar.FRIDAY:
				rtn = "금";
				break;
			case  Calendar.SATURDAY:
				rtn = "토";
				break;
			case  Calendar.SUNDAY:
				rtn = "일";
				break;
		}
		
		return rtn;
	}
}
